﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    public partial class frmSampleSend : Form
    {
        Entities db = new Entities();

        T_Send m_Send = null;

        T_Sample m_Sample = null;

        List<T_Sample> tSample = null;

        T_SendDetail m_sendDetail = null;

        public int[] jCount { get; set; }

        private bool IsQuery = true;

        public string sNo
        {
            set
            {
                var allSendList = db.T_Send.Join(db.T_SendDetail, a => a.sID, b => b.sID, (a, b) => new { a,b
                }).Join(db.T_Sample, c => c.b.xID, d => d.xID, (c, d) => new {c.a.sNo,d.productNo,d.opDate,d.size2,d.sKind,d.location,d.brand,d.contractNo,d.size1,d.property,d.seasonType,d.sCount,c.b.jCount,d.reMark,d.season,d.custDept});
                var sample = allSendList.Where(f => f.sNo == value).ToList();
                bsDetail.DataSource = sample.ToList();

                var sendlist = db.T_Send.Where(a => a.sNo == value);
                foreach (T_Send send in sendlist)
                {
                    m_Send = send;
                    break;
                }

                if (m_Send == null)
                {
                    return;
                }

                txtDsNo.Text = m_Send.sNo;
                dtTxtrequireTime.ValueText = m_Send.requireTime.ToString();
                dtTxtsendTime.ValueText = m_Send.sendTime.ToString();
                cbDcust.Text = m_Send.cust;
                txtcbsendCompany.Text = m_Send.sendCompany;
                txtcountry.Text = m_Send.country;
                txtreceiver.Text = m_Send.receiver;
                txtAddress.Text = m_Send.Address;
                txtedCompany.Text = m_Send.edCompany;
                txtSendDesc.Text = m_Send.sendDesc;
                txtAccount.Text = m_Send.account;
                txtDydNo.Text = m_Send.ydNo;
                txtWeight.Text = m_Send.weight.ToString();
                txtPrice.Text = m_Send.price.ToString();
                txtcName.Text = m_Send.cName;
                txtyName.Text = m_Send.yName;
                txtCompanyFullName.Text = m_Send.CompanyFullName;
                if (m_Send.payment == "预付") rbPayment1.Checked = true;
                if (m_Send.payment == "到付") rbPayment2.Checked = true;
                
            }
        }

        public int[] xID
        {
            set
            {
                tSample = new List<T_Sample>();
                for (int i = 0; i < value.Length;i++ )
                {
                    int id = value[i];
                    var list = db.T_Sample.Where(a => a.xID == id);
                   
                    tSample.Add(list.FirstOrDefault());
                }
                bsDetail.DataSource = tSample;

                string str = DateTime.Now.ToString("yyMMdd");
                var count = db.T_Send.Where(a => a.sNo.Contains(str)).Count();
                if (count < 9)
                {
                    txtDsNo.Text = str + "-0" + (count + 1).ToString();
                }
                else
                {
                    txtDsNo.Text = str + "-" + (count + 1).ToString();
                }
                dtTxtrequireTime.ValueText = DateTime.Now.ToString("yyyy/MM/dd");
                dtTxtsendTime.ValueText = DateTime.Now.ToString("yyyy/MM/dd");

            }
        }
        public frmSampleSend()
        {
            InitializeComponent();
            gvDataSource();
            panList.BringToFront();
            Service.Cust(cbcust);
            Service.Cust(cbDcust);
            Service.CBRemark(cbTxtremark);
        }

        private void gvDataSource()
        {
            DateTime dt = DateTime.Now.Date;
            var sendList = db.T_Send.Where(a=>a.requireTime==dt).OrderByDescending(b=>b.sNo).ToList();
            bslist.DataSource = sendList;

            bnav1.BindingSource = bslist;
            bnav2.BindingSource = bsDetail;
            gv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            gv2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        public void ShowPanDetail()
        {
            panDetail.BringToFront();
        }
        private void btnDetail_Click(object sender, EventArgs e)
        {
            gvID();
            if (this.IsQuery)
            {
                this.panDetail.BringToFront();
                this.btnDetail.Text = "概要";
                this.IsQuery = false;
                
            }
            else
            {
                this.panList.BringToFront();
                this.btnDetail.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void gv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.sNo = gv1.Rows[e.RowIndex].Cells["colsNo"].Value.ToString();
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch
            {
                Msg.ShowError("请选择表里的数据！");
            }
        }

        private void gvID()
        {
           
            if (gv1.RowCount > 0)
            {
                int index = gv1.CurrentRow.Index;    //取得选中行的索引  
                this.sNo = gv1.Rows[index].Cells["colsNo"].Value.ToString();
            }
            else
            {
                txtDsNo.Text = "";
                dtTxtrequireTime.ValueText = "";
                dtTxtsendTime.ValueText = "";
                cbDcust.Text = "";
                txtcbsendCompany.Text = "";
                txtcountry.Text = "";
                txtreceiver.Text = "";
                txtAddress.Text = "";
                txtedCompany.Text = "";
                txtSendDesc.Text = "";
                txtAccount.Text = "";
                txtDydNo.Text = "";
                txtWeight.Text = "";
                txtPrice.Text = "";
                txtcName.Text = "";
                txtyName.Text = "";
                txtCompanyFullName.Text = "";
                rbPayment1.Checked = true;
            }
            
        }

        private void SaveAdd()
        {
            
            m_Send = new T_Send();
            if (db.T_Send.Count() <= 0) m_Send.sID = 1;
            else m_Send.sID = db.T_Send.OrderByDescending(a => a.sID).First().sID + 1;
            m_Send.sNo = txtDsNo.Text;
            m_Send.requireTime = dtTxtrequireTime.Value;
            m_Send.sendTime = dtTxtsendTime.Value;
            m_Send.cust = cbDcust.Text;
            m_Send.sendCompany = txtcbsendCompany.Text;
            m_Send.country = txtcountry.Text;
            m_Send.receiver = txtreceiver.Text;
            m_Send.Address = txtAddress.Text;
            m_Send.edCompany = txtedCompany.Text;
            m_Send.sendDesc = txtSendDesc.Text;
            m_Send.account = txtAccount.Text;
            m_Send.ydNo = txtDydNo.Text;
            if (!string.IsNullOrEmpty(txtWeight.Text)) m_Send.weight = Convert.ToInt32(txtWeight.Text);
            if (!string.IsNullOrEmpty(txtPrice.Text)) m_Send.price = Convert.ToInt32(txtPrice.Text);
            m_Send.cName = txtcName.Text;
            m_Send.yName = txtyName.Text;
            m_Send.CompanyFullName = txtCompanyFullName.Text;
            if (rbPayment1.Checked == true) m_Send.payment = "预付";
            if (rbPayment2.Checked == true) m_Send.payment = "到付";

            db.T_Send.Add(m_Send);
            int i = db.SaveChanges();
            try
            {
                int j = 0;
                foreach (var list in tSample)
                {
                    m_Sample = list;
                    m_sendDetail = new T_SendDetail();
                    m_sendDetail.sdID = db.T_SendDetail.OrderByDescending(a => a.sdID).First().sdID + 1;
                    m_sendDetail.xID = m_Sample.xID;
                    m_sendDetail.sID = m_Send.sID;
                    m_sendDetail.jCount = jCount[j];
                    m_sendDetail.productNoImg = m_Sample.productNoImg;
                    //m_sendDetail.remark=;
                    db.T_SendDetail.Add(m_sendDetail);
                    db.SaveChanges();
                    j++;
                }
                Msg.ShowInfo(string.Format("已保存{0}条单号信息。", i));
            }
            catch (Exception e)
            {
                Msg.ShowError(e.Message);
            }
            
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void SaveModify()
        {
            if (m_Send == null) return;

            m_Send.sNo = txtDsNo.Text;
            m_Send.requireTime=dtTxtrequireTime.Value;
            m_Send.sendTime=dtTxtsendTime.Value;
            m_Send.cust = cbDcust.Text;
            m_Send.sendCompany = txtcbsendCompany.Text;
            m_Send.country = txtcountry.Text;
            m_Send.receiver = txtreceiver.Text;
            m_Send.Address = txtAddress.Text;
            m_Send.edCompany = txtedCompany.Text;
            m_Send.sendDesc = txtSendDesc.Text;
            m_Send.account = txtAccount.Text;
            m_Send.ydNo = txtDydNo.Text;
            if(!string.IsNullOrEmpty(txtWeight.Text)) m_Send.weight=Convert.ToDecimal(txtWeight.Text);
            if(!string.IsNullOrEmpty(txtPrice.Text)) m_Send.price=Convert.ToDecimal(txtPrice.Text);
            m_Send.cName = txtcName.Text;
            m_Send.yName = txtyName.Text;
            m_Send.CompanyFullName = txtCompanyFullName.Text;
            if (rbPayment1.Checked == true) m_Send.payment = "预付";
            if (rbPayment2.Checked == true) m_Send.payment = "到付";

            int i = db.SaveChanges();
            Msg.ShowInfo(string.Format("已保存{0}条单号信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信保存单号信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_Send == null)
                {
                    SaveAdd();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
                else
                {
                    SaveModify();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void frmSampleSend_Load(object sender, EventArgs e)
        {
            Service.Cust(cbcust);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnDetail_Click(null, null);
        }
    }
}
